<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "ArticleRevision.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "ArticleRevision.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "ArticleRevisions.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Article.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Articles.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Users.php";

require_once '../library/Utility/Converter.php';
require_once '../library/Utility/TimeHelper.php';

class Admin_ArticleRevisionController extends Zend_Controller_Action {
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $articleRevisionModel;
    private $articleModel;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////  
    public function init() {
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->articleModel = new Application_Models_Articles();
        $this->articleRevisionModel = new Application_Models_Article_Revisions();
        $this->modelUser = new End_Models_Users();
        
        //OTHER 
        $this->currentUser = $this->getInforUser();
        
        //VIEW 
        $this->view->currentUser = $this->currentUser; 
        /* Initialize action controller here */
    }
    
    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    ////////////////////      
    public function indexAction() {
        $article_id = $this->_getParam('article_id');
        $revisions = $this->articleRevisionModel->fetchArticleRevision($article_id);
        $this->view->articleRevisions = $revisions;
    }

    public function editAction() {
        $form = new Application_Forms_Article_Revision();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $restore = $this->getRequest()->getPost('Restore');
            if ($restore == 'Restore') {
                $revision_id = $this->getRequest()->getPost('revision_id');
                $param = array("revision_id" => $revision_id);
                $this->_helper->redirector("restore", "articlerevision", "admin", $param);
                return;
            }
            $preview = $this->getRequest()->getPost('Preview');
            if ($preview == 'Preview') {
                $revision_id = $this->getRequest()->getPost('revision_id');
                $param = array("revision_id" => $revision_id);
                $this->_helper->redirector("preview", "articlerevision", "admin", $param);
                return;
            }
            $delete = $this->getRequest()->getPost('Delete');
            if ($delete == 'Delete') {
                $revision_id = $this->getRequest()->getPost('revision_id');
                $param = array("revision_id" => $revision_id);
                $this->_helper->redirector("delete", "articlerevision", "admin", $param);
                return;
            }
        } else {
            $revision_id = $this->_getParam('revision_id');
            if ($revision_id > 0) {
                $revisionArray = Utility_Converter::objectToArray($this->articleRevisionModel->_find($revision_id));
                $form->populate($revisionArray);
            }
        }
    }

    public function restoreAction() {

        $revision_id = $this->_getParam('revision_id');
        $revision = Utility_Converter::objectToArray($this->articleRevisionModel->find($revision_id));
        $article_id = $revision[0]->article_id;

        $articles = Utility_Converter::objectToArray($this->articleModel->find($article_id));
        // get old article
        $article_old = $articles[0];
        $current_revision = $revision[0];

        // Set up variable
        $article_id = $article_old->article_id;
        $category_id = $current_revision->category_id;
        $title = $current_revision->title;
        $sub_title = $current_revision->sub_title;
        $slug = $current_revision->slug;
        $description = $current_revision->description;
        $content = $current_revision->content;
        $author = $current_revision->author;
        $icons = $current_revision->icons;
        $image_square = $article_old->image_square;
        $image_thumbnail = $article_old->image_thumbnail;
        $image_small = $article_old->image_small;
        $image_crop = $article_old->image_crop;
        $image_medium = $article_old->image_medium;
        $image_large = $article_old->image_large;
        $status = $article_old->status;
        $num_views = $article_old->num_views;
        $created_date = $article_old->created_date;
        $created_user_id = $article_old->created_user_id;
        $created_user_name = $article_old->created_user_name;
        // Need update these value
        $updated_date = Utility_Time_Helper::getCurrentTime();
        $updated_user_id = $article_old->updated_user_id;
        $updated_user_name = $article_old->updated_user_name;
        //
        $activate_user_id = $article_old->activate_user_id;
        $activate_user_name = $article_old->activate_user_name;
        $activate_date = $article_old->activate_date;
        $allow_comment = $article_old->allow_comment;
        $num_comments = $article_old->num_comments;
        $is_hot = $article_old->is_hot;
        $show_date = $article_old->show_date;
        $sticky = $article_old->sticky;
        $language = $article_old->language;

        $article_new = new Article(
                        $article_id,
                        $category_id,
                        $title,
                        $sub_title,
                        $slug,
                        $description,
                        $content,
                        $author,
                        $icons,
                        $image_square,
                        $image_thumbnail,
                        $image_small,
                        $image_crop,
                        $image_medium,
                        $image_large,
                        $status,
                        $num_views,
                        $created_date,
                        $created_user_id,
                        $created_user_name,
                        $updated_date,
                        $updated_user_id,
                        $updated_user_name,
                        $activate_user_id,
                        $activate_user_name,
                        $activate_date,
                        $allow_comment,
                        $num_comments,
                        $is_hot,
                        $show_date,
                        $sticky,
                        $language
        );

        $this->articleModel->_update($article_new);
        // Redirect
        $param = array("article_id" => $article_id);
        $this->_helper->redirector("index", "articlerevision", "admin", $param);
    }

    public function deleteAction() {
        $revision_id = $this->_getParam('revision_id');
        /* Delete article */
        $revision = Utility_Converter::objectToArray($this->articleRevisionModel->find($revision_id));
        $article_id = $revision[0]->article_id;
        $param = array("article_id" => $article_id);

        $this->articleRevisionModel->_delete($revision_id);
        $this->_helper->redirector("index", "articlerevision", "admin", $param);
    }

}